layui.config({
    base: "/layui/"
}).extend({
    index: 'lib/index'
}).use(['index', 'form', 'jquery', 'upload', 'table', 'laydate', 'util', 'table'], function () {
    var form = layui.form,
        upload = layui.upload,
        laydate = layui.laydate,
        table = layui.table,
        util = layui.util,
        admin = layui.admin;

    form.render();

    /**
     * 防重复设置
     * @type {boolean}
     */
    var edit_flag = true;

    table.render({
        elem: '#card_info_table'
        , url: '/cardInfo/queryData'
        , method: "post"
        , height: 500
        , limit: 50							//显示数目
        , limits: [50, 100]
        , cols: [[
            {field: 'id', width: 100, title: 'id', "align": "center",hide: true}
            , {field: 'activityId', width: 100, title: '活动id', "align": "center",hide: true}
            , {field: 'card_type', width: 100, title: '卡券类型', "align": "center", templet: function (d) {
                    if (d.card_type == 'DISCOUNT') {
                        return "折扣券";
                    } else {
                        return "代金券";
                    }
                }}
            , {field: 'discount', width: 130, title: '打折额度', "align": "center", templet: function (d) {
                    if (d.discount == 0.05) {
                        return "9.5折";
                    } else if(d.discount == 0.10){
                        return "9折";
                    }else if(d.discount == 0.15){
                        return "8.5折";
                    }else if(d.discount == 0.20){
                        return "8折";
                    }else if(d.discount == 0.25){
                        return "7.5折";
                    }else {
                        return "错误额度";
                    }
                }}
            , {field: 'title', width: 170, title: '卡券名', "align": "center"}
            , {field: 'notice', width: 160, title: '使用提醒', "align": "center"}
            , {field: 'quantity', width: 110, title: '库存数量', "align": "center"}
            , {field: 'fixed_term', width: 130, title: '领取后有效天数', "align": "center"}
            , {field: 'fixed_begin_term', width: 130, title: '领取后生效天数', "align": "center", templet: function (d) {
                    if(d.fixed_begin_term == 0){
                        return "立即生效";
                    }else if(d.fixed_begin_term == null){
                        return "";
                    }else {
                        return d.fixed_begin_term + "天后";
                    }
                }}
            // , {field: 'get_limit', width: 130, title: '每人可领取数量', "align": "center"}
            // , {field: 'use_limit', width: 130, title: '每人可使用数量', "align": "center"}
            , {field: 'accept_category', title: '可用商品', "align": "center",hide: true}
            , {field: 'reject_category', title: '不可用商品', "align": "center",hide: true}
            , {field: 'state',width: 130, title: '状态', "align": "center", templet: function (d) {
                    if(d.state == 0){
                        return "未激活"
                    }else if(d.state == 1){
                        return "已上线"
                    }else {
                        return "已下线"
                    }
                }}
            , {field: 'OPT', width: 180, title: '操作', "align": "center",toolbar: "#OptData"}
        ]]
        , request: {
            pageName: 'pageNo' //页码的参数名称，默认：page
            , limitName: 'pageSize' //每页数据量的参数名，默认：limit
        },
        where: {
            activityId: $('#activityId').val()
        }
        , id: 'card_info_table'
        , page: true
    });

    table.on('tool(card_info_table)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        var tr = obj.tr; //获得当前行 tr 的DOM对象
        if (layEvent === 'modify_card') {
            form.val('cpn_card_info_form', {
                "id": data.id,
                "title": data.title,
                "notice": data.notice,
                "quantity": data.quantity,
                // "get_limit": data.get_limit,
                // "use_limit": data.use_limit,
                "accept_category": data.accept_category,
                "reject_category": data.reject_category
            });

            admin.popup({
                title: '编辑'
                , shade: 0
                , anim: -1
                , offset: "20px"
                , area: ['60%', '600px']
                , skin: 'layui-anim layui-anim-upbit'
                , content: $("#add_card_info_win")
                , end: function () {
                    table.reload('card_info_table', {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                        , where: {}
                    });
                }
            });

        }else if(layEvent === 'start_card'){
            layer.confirm('请确认信息填写完整(包括优惠券门店信息补充、商家进件与服务设置),制券后将不可修改!!!', function (index) {
                $.ajax({
                    url: "/cardInfo/startMakeCard",
                    data: {"id": data.id},
                    type: "post",
                    success: function (result) {
                        if (result.code == 1) {
                            layer.msg(result.message, {
                                offset: '32%'
                                , icon: 1
                                , time: 3000
                            }, function () {

                            });
                            table.reload('card_info_table', {});
                        } else {
                            layer.msg(result.message, {
                                offset: '32%'
                                , icon: 2
                                , time: 3000
                            }, function () {

                            });
                        }
                    }
                });
            });
        }
    });

    /**
     * 编辑卡券信息
     */
    form.on('submit(editOk)', function (data) {
        if (edit_flag) {
            edit_flag = false;
            var title = $("#title").val();
            if(title.length > 9){
                layer.msg("卡券名称不能超过9个汉字", {
                    offset: '32%'
                    , icon: 2
                    , time: 3000
                }, function () {})
            }
            var notice = $('#notice').val();
            if(notice.length > 16){
                layer.msg("使用提示语不能超过16个汉字", {
                    offset: '32%'
                    , icon: 2
                    , time: 3000
                }, function () {})
            }
            $.ajax({
                url: "/cardInfo/editCardInfo",
                data: data.field,
                type: "post",
                success: function (result) {
                    if (result.code == 1) {
                        edit_flag = true;
                        layer.msg("编辑成功", {
                            offset: '32%'
                            , icon: 1
                            , time: 0
                            , btn: '知道了'
                            , btnAlign: "c"
                            , shade: 0.4
                            , yes: function () {
                                table.reload('card_info_table', {});
                                layer.closeAll();
                            }
                        })
                    } else {
                        edit_flag = true;
                        layer.msg(result.message, {
                            offset: '32%'
                            , icon: 2
                            , time: 3000
                        }, function () {
                            layer.closeAll();
                        })
                    }

                }
            });
        } else {
            layer.msg("不能重复提交表单", {
                offset: '32%'
                , icon: 2
                , time: 3000
            }, function () {

            })
        }

        return false;
    });

});



